package cn.com.mayue.guava.collections;

import cn.com.mayue.guava.Person;
import com.google.common.base.Function;
import com.google.common.base.Joiner;
import com.google.common.base.Predicate;
import com.google.common.collect.FluentIterable;
import com.google.common.collect.Iterables;
import com.google.common.collect.Lists;
import org.junit.Test;

import java.util.List;

/**
 * Created by mayue on 2016/12/22.
 */
public class FluentIterableTest {
    @Test
    public void testObjectOrder() {
        Person personOne = new Person("testOne", 10);
        Person personTwo = new Person("testTwo", 20);
        Person personThree = new Person("testThree", 30);
        List<Person> personList = Lists.newArrayList(personOne, personTwo, personThree);

        Iterable<Person> personsIte = FluentIterable.from(personList).filter(new Predicate<Person>() {
            public boolean apply(Person person) {
                return person.getAge() > 10;
            }
        });

        System.out.println(Iterables.contains(personsIte, personOne));
        System.out.println(Iterables.contains(personsIte, personTwo));
        System.out.println(Iterables.contains(personsIte, personThree));
    }

    @Test
    public void testTransform() {
        Person personOne = new Person("testOne", 10);
        Person personTwo = new Person("testTwo", 20);
        Person personThree = new Person("testThree", 30);
        List<Person> personList = Lists.newArrayList(personOne, personTwo, personThree);

        //基于已有集合 构建新的集合
        List<String> transformPersonList = FluentIterable.from(personList).transform(new Function<Person, String>() {
            public String apply(Person person) {
                return Joiner.on(':').join(person.getName(), person.getAge());
            }
        }).toList();

        System.out.println(transformPersonList);

    }
}
